<template>
  <div>
    <el-row>
      <el-dialog
        :title="title"
        :visible.sync="dialogVisible"
        width="50%"
      >
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="楼栋/房屋" prop="unitCode">
            <el-cascader
              size="mini"
              clearable
              v-model="buildingUnit"
              placeholder="请选择楼栋/单元"
              :props="props"
            >
            </el-cascader>
          </el-form-item>
          <el-form-item label="楼层" prop="roomLevel">
            <el-input v-model="form.roomLevel" placeholder="请输入楼层"/>
          </el-form-item>
            <el-form-item label="小区名称" prop="buildingId">
              <el-select v-model="form.communityId" placeholder="小区名称"  clearable size="mini">
                <el-option v-for="item in communityNameList"
                           :key="item.communityId"
                           :label="item.communityName"
                           :value="item.communityId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          <el-form-item label="房间编号" prop="roomName">
            <el-input v-model="form.roomCode" placeholder="请输入房间编号"/>
          </el-form-item>
          <el-form-item label="房屋名称" prop="roomName">
            <el-input v-model="form.roomName" placeholder="请输入房屋名称"/>
          </el-form-item>
          <el-form-item label="建筑面积" prop="roomAcreage">
            <el-input v-model="form.roomAcreage" placeholder="请输入建筑面积" />
          </el-form-item>
          <el-form-item label="房屋状态" prop="roomStatus">
            <el-select v-model="form.roomStatus" placeholder="请选择房屋状态">
              <el-option
                v-for="item in allRoomStatus"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否商铺" prop="buildingAcreage" >
            <el-select   v-model="form.roomIsShop" placeholder="是否商铺" >
              <el-option
                v-for="item in isShopList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否商品房" prop="roomSCommercialHouse">
            <el-select   v-model="form.roomSCommercialHouse" placeholder="是否商铺" >
              <el-option
                v-for="item in isShopList"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="房屋户型" prop="roomHouseType">
            <el-select v-model="form.roomHouseType" placeholder="请选择房屋户型" size="mini">
              <el-option
                v-for="item in allRoomType"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="备注"/>
          </el-form-item>
        </el-form>
        <el-row>
          <el-col :span="24" style="margin-left: 30%">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button style="margin-left: 8%" type="primary" @click="save">确 定</el-button>
          </el-col>
        </el-row>
      </el-dialog>
    </el-row>

    <el-form :model="queryFormParam" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="楼栋/房屋" prop="unitCode">
        <el-cascader
          size="mini"
          clearable
          v-model="buildingUnit"
          placeholder="请选择楼栋/单元"
          :props="props"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="房屋状态" prop="unitCode">
        <el-select v-model="queryFormParam.roomStatus" placeholder="请选择房屋状态" size="mini">
          <el-option
            v-for="item in allRoomStatus"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
            clearable
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" size="mini" @click="search()"><i
        class="el-icon-search"
      />搜索
      </el-button>
      <el-button size="mini" @click="reset"><i class="el-icon-refresh" style="color: #F38181"/>重置</el-button>
      <br>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="add">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="deleteBuildingByIds">
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" :disabled="multiple" size="mini" @click="exportTable">导出
        </el-button>
      </el-col>
    </el-row>
    <el-table
      ref="multipleTable"
      :data="roomPageList"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="房间编号"
        width="120"
      >
        <template slot-scope="scope">{{ scope.row.roomCode }}</template>
      </el-table-column>
      <el-table-column
        label="小区名称"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.zyCommunity.communityName }}</template>
      </el-table-column>
      <el-table-column
        label="楼栋名称"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.zyBuilding.buildingName }}</template>
      </el-table-column>
      <el-table-column
        label="单元名称"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.zyUnit.unitName }}</template>
      </el-table-column>
      <el-table-column
        label="楼层"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.roomLevel }}</template>
      </el-table-column>
      <el-table-column
        label="房间名称"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.roomName }}</template>
      </el-table-column>
      <el-table-column
        label="房屋建筑面积"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.roomAcreage }}</template>
      </el-table-column>
      <el-table-column
        label="算费系数"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.roomCost }}</template>
      </el-table-column>
      <el-table-column
        label="房屋状态"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.houseStatus }}</template>
      </el-table-column>
      <el-table-column
        label="是否商铺"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.isShop }}</template>
      </el-table-column>
      <el-table-column
        label="是否商品房"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.isShopHouse }}</template>
      </el-table-column>
      <el-table-column
        label="房屋类型"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.houseType }}</template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.createTime }}</template>
      </el-table-column>
      <el-table-column
        label="备注"
        width="150"
      >
        <template slot-scope="scope">{{ scope.row.remark }}</template>
      </el-table-column>

      <el-table-column
        label="操作"
        width="150"
      >
        <template slot-scope="scope">
          <a href="#" style="color: dodgerblue" @click="updateOne(scope.row)" ><i class="el-icon-edit">修改</i></a>
          <a href="#" style="color: dodgerblue" @click="deleteOne(scope.row)"><i class="el-icon-delete-solid">删除</i></a>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page="this.currentPage"
      :page-sizes="[4, 8, 14, 20]"
      :page-size="this.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="this.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import axios from "axios";
import log from "@/views/log";

export default {
  name: 'RoomViewVue',
  data() {
    return {
      roomPageList: [],
      form: {"roomId":"","communityId":"","buildingId":"","unitId":"","roomLevel":"","roomCode":"","roomName":"","roomAcreage":"","roomCost":"","roomStatus":"","roomIsShop":"","roomSCommercialHouse":"","roomHouseType":"","remark":""},
      buildingUnit: [],
      isShopList: [],
      allRoomType:[],
      queryFormParam: {
        "roomId": "",
        "communityId": "",
        "buildingId": "",
        "unitId": "",
        "roomLevel": "",
        "roomCode": "",
        "roomName": "",
        "roomAcreage": "",
        "roomCost": "",
        "roomStatus": "",
        "roomIsShop": "",
        "roomSCommercialHouse": "",
        "roomHouseType": "",
        "createBy": "",
        "createTime": "",
        "updateBy": "",
        "updateTime": "",
        "remark": "",
      },
      total: 0,
      pageSize: 4,
      currentPage: 1,
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      //选中为禁用
      isSelected: false,
      formLabelWidth: '120px',
      dialogTableVisible: false,
      dialogFormVisible: false,
      dialogVisible: false,
      title: "",
      Bid: '',
      Uid: "",
      roomStatusList: [],
      allRoomStatus: [],
      communityNameList:[],
      props: {
        lazy: true,
        lazyLoad(node, resolve) {
          setTimeout(() => {
            //第一级
            if (node.level === 0) {
              axios.get("/building/all")
                .then(res => {
                  const nodes = res.data.data.map((item, index) => ({
                    value: item.buildingId,
                    label: item.buildingName,
                  }));
                  resolve(nodes);
                })
            }
            //第二级
            if (node.level === 1) {
              console.log(node.value)
              axios.get("/unit/getZyUnitByBuildingId/" + node.value)
                .then(res => {
                  const nodes = res.data.data.map((item, index) => ({
                    value: item.unitId,
                    label: item.unitName,
                    leaf: node.level == 1
                  }));
                  resolve(nodes);
                })
            }
          }, 200);
        }
      },


    }
  }, methods: {
    loadRoomPageList(currentPage, pageSize) {
      this.axios.post("/room/pageList?currentPage=" + currentPage + "&pageSize=" + pageSize, {})
        .then(res => {
          this.roomPageList = res.data.data.records
          this.total = res.data.data.total
        })

    }, getAllRoomStatus() {
      this.axios.get("/intelligence_community/sys-dict-data/roomStatus").then(res => {
        this.allRoomStatus = res.data.data;
        console.log(this.allRoomStatus)
      })
    },getAllRoomType() {
      this.axios.get("/intelligence_community/sys-dict-data/allRoomType").then(res => {
        this.allRoomType = res.data.data;
      })
    }
    , getAllIsShop() {
      this.axios.get("/intelligence_community/sys-dict-data/isShop").then(res => {
        this.isShopList = res.data.data;
      })
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.roomId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      this.isSelected = selection.length !== 0
    }, handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.loadRoomPageList(this.currentPage, pageSize)
    },
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage
      this.loadRoomPageList(currentPage, this.pageSize)
    }, loadRoomStatusList() {
      this.axios.get("/room/getRoomStatusList").then(res => {
        this.roomStatusList = res.data.data
      })
    }, loadBuildingAndUnit() {
      this.axios.get("/building/unitList/0")
        .then(res => {
          // 将获得到的数据赋值给options
          this.options = res.data.data;
        });
    },

    search() {
      this.Bid = this.buildingUnit[0]
      this.Uid = this.buildingUnit[1],
      this.axios.post("/room/pageList?currentPage=" + this.currentPage + "&pageSize=" + this.pageSize, {
        "buildingId": this.Bid,
        "unitId": this.Uid,
        "roomStatus": this.queryFormParam.roomStatus
      }).then(res => {
        this.roomPageList = res.data.data.records;
        this.total = res.data.data.total

      })

    }, reset() {
      this.Bid = ''
      this.Uid = ''
      this.queryFormParam.roomStatus = ''
      this.loadRoomPageList(this.currentPage, this.pageSize)

    }, handleChange(value) {
      console.log(value)

      this.axios.get("/building/unitList/" + this.selectedOptions).then(res => {
        this.props.children = res.data.data.children;
        this.props.value = 'unitId'
        this.props.label = 'unitName'
        console.log(this.props)
        console.log(res.data.data)
      })
    }, add() {
      this.dialogVisible = true;
    }, updateOne(row) {
      console.log(row)
      this.dialogVisible=true;
      this.buildingUnit[0]=row.buildingId
      this.buildingUnit[1]=row.unitId
      this.form.roomSCommercialHouse=row.roomSCommercialHouse
      this.form.roomIsShop=row.roomIsShop
        this.form=row;
    },save() {
      if(this.form.roomIsShop=="是"){
        this.form.roomIsShop="Y"
      }else{
        this.form.roomIsShop="N"
      }
      if(this.form.roomSCommercialHouse=="是"){
        this.form.roomSCommercialHouse="Y"
      }else{
        this.form.roomSCommercialHouse="N"
      }
      this.form.buildingId=this.buildingUnit[0]
      this.form.unitId=this.buildingUnit[1]
      console.log(this.form)
      if (this.form.roomId == "") {

        // console.log(this.buildingUnit)
        this.axios.post("/room/add",{"roomId":this.form.roomId,"communityId":this.form.communityId,"buildingId":this.form.buildingId,"unitId":this.form.unitId,"roomLevel":this.form.roomLevel,"roomCode":this.form.roomCode,"roomName":this.form.roomName,"roomAcreage":this.form.roomAcreage,
          "roomCost":this.form.roomCost,"roomStatus":this.form.roomStatus,"roomIsShop":this.form.roomIsShop,"roomSCommercialHouse":this.form.roomSCommercialHouse,"roomHouseType":this.form.roomHouseType}).then(res=>{
          if (res.data.data === '添加成功') {
            this.$message({
              message: '添加成功!',
              type: 'success',
              center: 'true'
            })
          }
          this.form={roomId:"",communityId:"",buildingId:"",unitId:"",roomLevel:"",roomCode:"","roomName":"",roomAcreage:"",roomCost:"",roomStatus:"",roomIsShop:"",roomSCommercialHouse:"",roomHouseType:""}
          this.buildingUnit=[];
          this.loadRoomPageList(this.currentPage,this.pageSize)
          }
        )
      } else {
        console.log(this.form)
        this.axios.post("/room/update",{"roomId":this.form.roomId,"communityId":this.form.communityId,"buildingId":this.form.buildingId,"unitId":this.form.unitId,"roomLevel":this.form.roomLevel,"roomCode":this.form.roomCode,"roomName":this.form.roomName,"roomAcreage":this.form.roomAcreage,
          "roomCost":this.form.roomCost,"roomStatus":this.form.roomStatus,"roomIsShop":this.form.roomIsShop,"roomSCommercialHouse":this.form.roomSCommercialHouse,"roomHouseType":this.form.roomHouseType,"remark":this.form.remark}).then(res=>{
            if(res.data.data==="修改成功"){
              this.$message({
                message: '修改成功!',
                type: 'success',
                center: 'true'
              })
            }
          this.dialogVisible=false;
          this.form={roomId:"",communityId:"",buildingId:"",unitId:"",roomLevel:"",roomCode:"","roomName":"",roomAcreage:"",roomCost:"",roomStatus:"",roomIsShop:"",roomSCommercialHouse:"",roomHouseType:""}
          this.buildingUnit=[];
          this.loadRoomPageList(this.currentPage,this.pageSize)
          }
        )
      }
    },
    deleteBuildingByIds() {
      this, axios.delete("/room/delByIds", this.ids).then(res => {
        if (res.data.data == "删除成功") {
          this.$message({
            message: '删除成功!',
            type: 'success',
            center: 'true'
          })
        }
        this.loadRoomPageList(this.currentPage, this.pageSize)

      })

    },
  exportTable(row) {
      const roomId = row.roomId || this.ids
      this.axios.post('/room/excel', roomId).then(res => {
        console.log(res.data.data)
        if (res.data.data === '导出成功') {
          this.$message({
            message: '已成功导入桌面',
            type: 'success'
          })
        }
      })
    },
  deleteOne(row) {
      this.axios.delete("/room/delOne/" + row.roomId).then(res => {
        if (res.data.data == "删除成功") {
          this.$message({
            message: '删除成功!',
            type: 'success',
            center: 'true'
          })
        }
      })
      this.loadRoomPageList(this.currentPage, this.pageSize)
    },loadCommunityNameList() {
      this.axios.get("/community/all").then(res => {
        this.communityNameList = res.data.data;
      })
    }
  }, created() {
    this.loadRoomPageList(this.currentPage, this.pageSize)
    this.loadRoomStatusList();
    this.loadBuildingAndUnit();
    this.getAllRoomStatus();
    this.getAllIsShop()
    this.getAllRoomType()
    this.loadCommunityNameList()
  }
}
</script>

<style scoped>

</style>
